package com.wh.wisdomsite.xbox.core.event;

import com.wh.wisdomsite.xbox.core.util.GlobalThreadPool;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/***
 *
 * @Package com.wh.wisdomsite.xbox.entity.client
 * @author 谢泽鹏
 * @date 2016年11月07日 下午15:10
 * @Copyright 个人版权所有
 * @Company 贵州万华科技有限公司Copyright (c) 2016
 * @version V1.0
 */
public class SimpleXboxEventMulticaster extends AbstractXboxEventMulticaster {

    private static final Log logger = LogFactory.getLog(SimpleXboxEventMulticaster.class);

    private boolean asyn;

    public SimpleXboxEventMulticaster(boolean asyn) {
        this.asyn = asyn;
    }

    /**
     * 分发事件
     * @param event
     */

    @Override
    public void multicaster(final XboxEvent event) {
        try {
            for (final XboxEventListener listener : getXboxEventListeners(event)) {
                if (asyn) {
                    GlobalThreadPool.submitTask(new Runnable() {
                        @Override
                        public void run() {
                            listener.handleEvent(event);
                        }
                    });
                } else {
                    listener.handleEvent(event);
                }

            }
        } catch (Exception e) {
            if (logger.isErrorEnabled()) {
                logger.error("事件拦截器" + event.getClass() + "处理时报错,并中断后续拦截器", e);
            }
        }
    }

}
